/*
 * Copyright 2010. 
 * 
 * This document may not be reproduced, distributed or used 
 * in any manner whatsoever without the expressed written 
 * permission of xrkol Corp. 
 * 
 * $Rev: Rev $
 * $Author: Author $
 * $LastChangedDate: LastChangedDate $
 *
 */

package com.xrkol.sharelib.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.xrkol.sharelib.dao.TeacherDao;
import com.xrkol.sharelib.entity.Teacher;

/**
 * @author peng.xia
 *
 */
@Repository
public class TeacherDaoImpl extends BaseDaoImpl<Teacher, String> implements
		TeacherDao {

	@Override
	public Teacher findByUserName(String string) {
		String queryString = "from Teacher u where u.username=?";
		List<Teacher> users = executeQueryWithoutPaging(queryString, string);
		return users.size() > 0 ? users.get(0) : null;
	}

	@Override
	public List<Teacher> findTeachersWithPagination(String schoolId,
			Integer page) {
		String queryString = "from Teacher t where t.schoolId=?";
		return executeQueryWithPagination(queryString, " order by id desc",
				page, schoolId);
	}

	@Override
	public List<Teacher> getAllTeachers(String schoolId) {
		String queryString = "from Teacher t where t.schoolId = ?";
		return executeQueryWithoutPaging(queryString, schoolId);
	}

	@Override
	public Teacher findByUserId(String userId) {
		String queryString = "from Teacher t where t.userId = ?";
		List<Teacher> teachers = executeQueryWithoutPaging(queryString, userId);
		return teachers.size() > 0 ? teachers.get(0) : null;
	}

	@Override
	public boolean checkExist(Teacher teacher) {
		String queryString = "from Teacher t where t.realname = ? and t.tel = ?";
		List<Teacher> teachers = executeQueryWithoutPaging(queryString,
				teacher.getRealname(), teacher.getTel());
		return teachers.isEmpty() ? false : true;
	}

	@Override
	public List<Teacher> findManagersWithPaging(String schoolId, Integer page) {
		String queryString = "from Teacher t where t.schoolId=? and t.isAdmin=true ";
		return executeQueryWithPagination(queryString, " order by t.realname desc",
				page, schoolId);
	}
	
}
